import { ProFormSelectProps, ProFormTreeSelect } from '@ant-design/pro-components';
import React from 'react';
import { getApiData } from '@/util/util';
import { getTreeList } from '@/services/system/org';

type MyOrgSelectProps = {
  required?: boolean;
  requiredMsg?: string;
} & ProFormSelectProps;
const MyOrgSelect: React.FC<MyOrgSelectProps> = ({
  label = '机构',
  name = 'orgId',
  required = true,
  requiredMsg = '机构为必填项',
  fieldProps = undefined,
  ...restProps
}) => {
  const props: { fieldNames: { children: string; label: string; value: string } } = {
    ...fieldProps,
    fieldNames: {
      label: 'title',
      value: 'id',
      children: 'children',
    },
  };
  return (
    <ProFormTreeSelect
      label={label}
      name={name}
      fieldProps={props}
      request={() => getApiData(getTreeList)}
      rules={[
        {
          required: required,
          message: requiredMsg,
        },
      ]}
      {...restProps}
    />
  );
};

export default MyOrgSelect;
